#include <plugin_test/qls_plugin_test.h>
#include "ui_qls_plugin_test.h"

#include "lottie_widget.h"

#include "dynamic_json_ui/dynamic_json_ui.h"

using namespace dynamic_json_ui;

QLsPluginTest::QLsPluginTest(QWidget *parent)
    : QWidget(parent), ui(new Ui::QLsPluginTest)
{
    ui->setupUi(this);

    //    // 设置布局
    //    // 创建 LottieWidget
    //    wd_lottie_ = new LottieWidget(this);

    //    // 加载 Lottie 动画 JSON 文件
    //    wd_lottie_->loadLottie("lottie:rect_rotate.json");

    //    // 设置 LottieWidget 的大小
    //    wd_lottie_->setFixedSize(400, 400);

    //    // 添加 LottieWidget 到布局
    //    ui->verticalLayout->addWidget(wd_lottie_);

    //    // 启动动画
    //    wd_lottie_->startAnimation();

    DynamicJsonUI *dynamic_json_ui = new DynamicJsonUI;
    dynamic_json_ui->resize(400, 500);

    QString json = R"({
            "type": "VBoxLayout",
            "style": "background: #f0f0f0; padding: 10px;",
            "children": [
                { "type": "Label", "id": "label1", "properties" : { "text": "用户名" } },
                { "type": "LineEdit", "id": "username", "properties": { "placeholderText": "请输入用户名" } },
                { "type": "ComboBox", "id": "combo1", "properties" : { "items": ["选项A", "选项B", "选项C"], "currentIndex" : 1 } },
                { "type": "CheckBox", "id": "checkBox1", "properties" : { "text": "是否同意协议", "checked": false } },
                { "type": "Slider", "id": "slider1", "properties" : { "minimum": 0, "maximum": 100, "value": 50 } },
                { "type": "SpinBox", "id": "spin1", "properties" : { "minimum": 1, "maximum": 100, "value": 10 } },
                { "type": "Button", "id": "submit", "properties" : { "text": "提交" }, "on": { "Clicked": "submitForm" } }
            ]
        })";

    QObject::connect(
        dynamic_json_ui, &DynamicJsonUI::callJsFunction,
        [](const QString &name) { qDebug() << "调用了函数：" << name; });

    dynamic_json_ui->loadFromJsonString(json);

    // 修改 Label 文本示例
    dynamic_json_ui->setPropertyValue("label1", "text", "新用户名");

    ui->verticalLayout->addWidget(dynamic_json_ui);
}

QLsPluginTest::~QLsPluginTest()
{
    delete ui;
}

void QLsPluginTest::on_pushButton_clicked()
{
    //    wd_lottie_->startAnimation();
}
